from django.shortcuts import render_to_response,get_object_or_404

from django.core.paginator import Paginator
from django.conf import settings
from django.db.models import Count

from .models import Blog,BlogType

from read_count.utils import read_count_once_read



page_of_each_blog =  settings.PAGE_OF_EACH_BLOG

def get_blog_list_common_data(request,blogs_all_list):
    paginator = Paginator(blogs_all_list,page_of_each_blog) #每10篇文章分页
    page_num = request.GET.get('page',1) #获取页面参数（get请求）
    page_of_blogs = paginator.get_page(page_num)
    current_page_num = page_of_blogs.number#获取当前页码
    #获取当前页码前后各两页的页码
    page_range=list(range( max(current_page_num-2,1) ,current_page_num) )+ list(range(current_page_num, min(current_page_num+2,paginator.num_pages)+1))
    #加上省略页码标记
    if page_range[0]>=3:
        page_range.insert(0,'...')
    if paginator.num_pages-page_range[-1]>=2:
        page_range.append('...')
    #加上首页尾页
    if page_range[0] !=1:
        page_range.insert(0,1)
    if page_range[-1] != paginator.num_pages:
        page_range.append(paginator.num_pages)
    
    #获取日期归档对应的博客数量
    blog_dates = Blog.objects.dates('created_time','month',order='DESC')
    blog_dates_dict ={}
    for blog_date in blog_dates:
        blog_count = Blog.objects.filter(created_time__year = blog_date.year,
                                         created_time__month = blog_date.month).count()
        blog_dates_dict[blog_date] = blog_count
    
    '''
    blog_types = BlogType.objects.all()
    views_blog_types_list =[]
    for xx in blog_types:
        xx.blog_count = Blog.objects.filter(blog_type=xx).count()
        views_blog_types_list.append(xx)
    '''
    context={}
    context['views_page_of_blogs'] = page_of_blogs
    #context['views_blogs'] = Blog.objects.all()
    context['views_blogs'] = page_of_blogs.object_list  
    context['page_range'] = page_range
    context['views_blogs_count'] = Blog.objects.all().count()
    #context['views_blog_types'] = views_blog_types_list
    context['views_blog_types'] =BlogType.objects.annotate(blog_count = Count('blog')) #获取博客分类的对应博客数量
    context['views_blog_dates'] = blog_dates_dict
    return context


def blog_list(request):
    blogs_all_list = Blog.objects.all()
    context = get_blog_list_common_data(request,blogs_all_list)
    return render_to_response('blog_list.html',context)
    

    
def blogs_with_type(request,blog_type_pk):

    blog_type = get_object_or_404(BlogType,pk=blog_type_pk)
    blogs_all_list = Blog.objects.filter(blog_type=blog_type)
    
    context = get_blog_list_common_data(request,blogs_all_list)
    
    context['views_blog_type'] = blog_type
    return render_to_response('blog_type.html',context)
    
def blogs_with_date(request,year,month):
  
    #blog_type = get_object_or_404(BlogType,pk=blog_type_pk)
    
    blogs_all_list = Blog.objects.filter(created_time__year=year, created_time__month=month)
    
    
    context = get_blog_list_common_data(request,blogs_all_list)
    context['views_blog_date'] = "%s年%s月" % (year,month)
    return render_to_response('blog_date.html',context)
    
    
def blog_detail(request,blog_pk):
    #return render_to_response('blog_detail.html',context)
    blog = get_object_or_404(Blog, pk=blog_pk)
    read_cookie_key = read_count_once_read(request,blog)

    
    context={}
    context['views_blog'] = blog
    context['views_previous_blog'] = Blog.objects.filter(created_time__gt=blog.created_time).last()
    context['views_next_blog'] = Blog.objects.filter(created_time__lt=blog.created_time).first()
    response =  render_to_response('blog_detail.html',context)
    response.set_cookie(read_cookie_key ,'true') #阅读cookie标记
    
    return response
 